package org.smvcd1.controller;

import org.apache.ibatis.annotations.Param;
import org.smvcd1.pojo.Person;
import org.smvcd1.pojo.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;


@Controller
@RequestMapping("/test5")
public class Test5Controller {

    @RequestMapping("/index1")
    public ModelAndView index1(HttpServletRequest request, HttpServletResponse response){

//        System.out.println(request.getParameter("id"));
//        try {
//            response.setContentType("text/html;charset=UTF-8");
//            request.setCharacterEncoding("UTF-8");
//            response.setCharacterEncoding("UTF-8");
//
//            response.getWriter().print("<b>你好！</b>");
//            response.getWriter().close();
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        request.setAttribute("msg","后添加的数据1");
        ModelAndView modelAndView=new ModelAndView("/test5_index1");
        modelAndView.addObject("msg3","456");
        return modelAndView;
    }

    @RequestMapping("/index2")
    public String index2(Model model, Integer id, String userName){
        model.addAttribute("msg2","123");
        System.out.println("id="+id+";userName="+userName);
        return "/test5_index2";
    }


    @RequestMapping("/index3")
    public String index3(HttpServletRequest request,Integer id,String userName){

        System.out.println("id="+request.getParameter("id")+";userName="+userName);
        return "/test5_index2";
    }

    @RequestMapping("/index4")
    public String index4(HttpSession session, Integer id, String userName){

        session.setAttribute("uname","张飞");

        System.out.println("id="+id+";userName="+userName+";登录名="+session.getAttribute("uname"));
        return "/test5_index2";
    }

    @RequestMapping("/login")
    public String Login(){
     return "/test5_login";
    }

//    @RequestMapping("/dologin")
//    public String doLogin(Model model, @RequestParam(value = "txtuName",required = true) String uName, String pwd){
//        if (uName.equals("admin")){
//            if (pwd.equals("123")){
//                model.addAttribute("msg","登录成功");
//            }else{
//                model.addAttribute("msg","密码错误");
//            }
//
//        }else{
//            model.addAttribute("msg","登录失败");
//        }
//        return "/test5_dologin";
//    }

//    @RequestMapping("/dologin")
//    public String doLogin(Model model, UserInfo userInfo, Person person){
//        System.out.println(person.getName());
//        if (userInfo.getuName().equals("admin")){
//            if (userInfo.getPwd().equals("123")){
//                model.addAttribute("msg","登录成功");
//            }else{
//                model.addAttribute("msg","密码错误");
//            }
//
//        }else{
//            model.addAttribute("msg","登录失败");
//        }
//        return "/test5_dologin";
//    }


    @RequestMapping("/dologin")
    public String doLogin(Model model,UserInfo userInfo, Person person, Map<String,Object> map){
        System.out.println(person.getName());
        if (userInfo.getuName().equals("admin")){
            if (userInfo.getPwd().equals("123")){
                //model.addAttribute("msg","登录成功");
                map.put("msg","登陆成功！");
            }else{
                map.put("msg","密码错误！");
            }

        }else{
            map.put("msg","用户名错误！");
        }
        return "/test5_dologin";
    }

}
